package ro.bluecube.admlocal.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import ro.bluecube.admlocal.gui.commons.AbstractController;
import ro.bluecube.admlocal.gui.commons.AbstractForm;
import ro.bluecube.admlocal.gui.users.UsersController;

public class MainFrame {

	protected static final int ADMINISTRARE_ACTION = 0;

	protected static final int ARBITRI_ACTION = 2;

	protected static final int MECIURI_ACTION = 3;

	protected static final int SPONSORI_ACTION = 4;

	protected static final int RAPOARTE_ACTION = 5;

	private static MainFrame instance;

	private static JFrame frame;

	private static String currentController = "";

	private Collection menuItems;

	private static JSplitPane jsp;

	private static JPanel buttonsPanel;

	private static JPanel generalPanel;

	private MainFrame() {
		frame = new JFrame("Administratia locala");
		frame.setVisible(false);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.requestFocus();
		initComponents();
		frame.setVisible(true);
		frame.requestFocus();
	}

	public static MainFrame getInstance() {
		if (instance == null) {
			instance = new MainFrame();
		}
		return instance;
	}

	private void initComponents() {
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
		buttonsPanel = new JPanel();
		generalPanel = new JPanel();
		generalPanel.setLayout(new BorderLayout());
		JPanel mainPanel = new JPanel(new GridBagLayout());
		mainPanel.add(generalPanel, new GridBagConstraints(0, 1, 1, 1, 1.0,
				1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH,
				new Insets(2, 2, 2, 2), 0, 0));
		jsp.add(buttonsPanel, JSplitPane.LEFT);
		jsp.add(mainPanel, JSplitPane.RIGHT);
		frame.add(jsp);
		buttonsPanel.setLayout(new BorderLayout());
		jsp.getLeftComponent().setPreferredSize(new Dimension(140, 40));
	}

	public void setMenuItems(Collection menuItems) {
		this.menuItems = menuItems;
		int i = 0;
		Dimension dim = new Dimension(100, 30);
		JMenuBar menuBar = new JMenuBar();
		menuBar.setPreferredSize(dim);
		JMenu menu = new JMenu("START");
		for (Iterator iter = menuItems.iterator(); iter.hasNext();) {
			JMenuItem element = (JMenuItem) iter.next();
			element.setPreferredSize(dim);
			element.setMinimumSize(dim);
			menu.add(element);
			i++;
		}
		menuBar.add(menu);
		frame.setJMenuBar(menuBar);
		frame.validate();
		frame.repaint();
	}

	protected void procesEvent(ActionEvent e) {
		if (e.getID() == ADMINISTRARE_ACTION) {
			activateController(UsersController.class.getName());
		}
		// TODO - trebe imbunatatita cumva modalitatea de instantiere a
		// controller-lor
//		else if (e.getID() == MECIURI_ACTION) {
//			activateController("gui.meciuri.MeciuriController");
//		} else if (e.getID() == RAPOARTE_ACTION) {
//			activateController("gui.rapoarte.RapoarteController");
//		} else if (e.getID() == ARBITRI_ACTION) {
//			activateController("gui.arbitri.ArbitriController");
//		} else if (e.getID() == RAPOARTE_ACTION) {
//			activateController("gui.echipe.EchipeController");
//		}
	}

	public static void setForm(AbstractForm form) {
		generalPanel.removeAll();
		generalPanel.add(form, BorderLayout.CENTER);
		buttonsPanel.removeAll();
		buttonsPanel.add(form.getToolBar(), BorderLayout.CENTER);
		jsp.validate();
		jsp.repaint();
	}

	public static JPanel getGeneralPanel() {
		return generalPanel;
	}

	public static void activateController(String className) {
		Class clazz = null;
		try {
			if (!currentController.equals(className)) {
				clazz = Class.forName(className);
				if (clazz != null) {
					try {
						((AbstractController) clazz.newInstance())
								.activate(new HashMap());
						currentController = className;
					} catch (InstantiationException e1) {
						return;
					} catch (IllegalAccessException e1) {
						return;
					}
				}

			}
		} catch (ClassNotFoundException e1) {
			System.out.println(e1.getMessage());
			return;
		}

	}
	public static JFrame getFrame(){
		return frame;
	}
}
